package com.yungam.other.mes_controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.gson.Gson;
import com.yungam.common.bean.product.ProductionProduct;
import com.yungam.common.service.ProductionProductService;
import com.yungam.other.response.ResData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * 完工信息同步接口
 */
@Slf4j
@RestController
public class ProductionFinishedController {

    @Resource
    private ProductionProductService productionProductService;

    @PostMapping("/IMCS/productionFinished")
    public Map<String, Object> productionFinished(@RequestBody Map<String, Object> params) {

        log.info("========= START『mes调用imcs』[完工信息同步接口]=========");
        log.info("=========『mes数据』{}", new Gson().toJson(params));

        // 处理参数
        String productSn = String.valueOf(params.get("productSn"));
        Integer good = (Integer) params.get("state");
        long finishTime = Long.parseLong(String.valueOf(params.get("finishTime")));
        Date endTime = new Date(finishTime);
        // 更新 production_product 表
        UpdateWrapper<ProductionProduct> uw = new UpdateWrapper<>();
        uw.eq("product_sn", productSn).set("state", 1).set("good", good).set("end_time", endTime);
        productionProductService.update(uw);

        log.info("同步成功");
        return ResData.success("同步成功");
    }

}
